package ah.dom;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Vector;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

//import org.apache.crimson.tree.XmlDocument;
import org.apache.crimson.tree.XmlDocument;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;

import com.sun.org.apache.xerces.internal.xni.XMLDocumentHandler;

public class Test {
	private Vector<StudentBean> student_Vector = new Vector<StudentBean>();
	
	
	/**
	 * @return student_Vector
	 */
	private Vector<StudentBean> getStudent_Vector() {
		return student_Vector;
	}


	/**
	 * @param student_Vector
	 *            要设置的 student_Vector
	 */
	private void setStudent_Vector(Vector<StudentBean> student_Vector) {
		this.student_Vector = student_Vector;
	}


	private void readXMLFile(String inFile) throws Exception {
		
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = null;
		
		db = dbf.newDocumentBuilder();
		
		Document doc = null;
		
		doc = db.parse(inFile);
		
		Element root = doc.getDocumentElement();
		
		NodeList students = root.getElementsByTagName("student");
		for (int i = 0; i<students.getLength(); i++) {
			Element student = (Element) students.item(i);
			StudentBean studentBean = new StudentBean();
			 studentBean.setSex(student.getAttribute("sex"));
			 
			 NodeList names = student.getElementsByTagName("name");
			 if (names.getLength() == 1) {
				 Element e = (Element) names.item(0);
				 Text t = (Text) e.getFirstChild();
				 studentBean.setName(t.getNodeValue());
			 }
			 
			 NodeList ages = student.getElementsByTagName("age");
			 if (ages.getLength() == 1) {
				 Element e = (Element) ages.item(0);
				 Text t = (Text) e.getFirstChild();
				 studentBean.setAge(Integer.parseInt(t.getNodeValue()));
			 }
			 
			 NodeList phones = student.getElementsByTagName("name");
			 if (phones.getLength() == 1) {
				 Element e = (Element) names.item(0);
				 Text t = (Text) e.getFirstChild();
				 studentBean.setPhone(t.getNodeValue());
			 }

			 student_Vector.add(studentBean);
		}
	} 
	
	
	private void writeXMLFile(String outFile) throws Exception {
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = null;
		
		db = dbf.newDocumentBuilder();
		
		Document doc = null;
		doc = db.newDocument();
		
		Element root = doc.createElement("students");
		
		doc.appendChild(root);
		
		for (int i=0; i<student_Vector.size(); i++) {
			StudentBean studentBean = (StudentBean)student_Vector.get(i);
			Element student = doc.createElement("student");
			student.setAttribute("sex", studentBean.getSex());
			root.appendChild(student);
			
			Element name = doc.createElement("name");
			student.appendChild(name);
			Text tName = doc.createTextNode(studentBean.getName());
			name.appendChild(tName);
			
			Element age = doc.createElement("age");
			student.appendChild(age);
			Text tAge = doc.createTextNode(studentBean.getAge()+"");
			age.appendChild(tAge);
			
			Element phone = doc.createElement("phone");
			student.appendChild(phone);
			Text tPhone = doc.createTextNode(studentBean.getPhone());
			phone.appendChild(tPhone);
			
		}
		//		 输出
		FileOutputStream fileOut = new FileOutputStream(outFile);
		OutputStreamWriter outWriter = new OutputStreamWriter(fileOut);
		
		((XmlDocument)doc).write(outWriter);
		
		fileOut.close();
		outWriter.close();
	}
	public static void main(String[] args) {
		Test test = new Test();
		Vector<StudentBean> student_vector = new Vector<StudentBean>();
		
		StudentBean o = new StudentBean();
		o.setSex("男");
		o.setAge(23);
		o.setName("张三");
		o.setPhone("12313241");
		
		StudentBean o1 = new StudentBean();
		o1.setAge(23);
		o1.setName("张三");
		
		student_vector.add(o);
		student_vector.add(o1);
		
		test.setStudent_Vector(student_vector);
		
		try {
			test.writeXMLFile("src/dom/outFile.xml");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
